﻿using System;
using NLite.Mini.Activation;
using NLite.Mini.Context;

namespace NLite.Mini.Lifestyle
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class LifestyleManagerAdapter:MarshalDisposable,ILifestyleManager
    {
        /// <summary>
        /// 
        /// </summary>
        public IKernel Kernel { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public IActivator Activator { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public IComponentInfo Info { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public Action<IComponentInfo, object> OnDestroying { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="activator"></param>
        /// <param name="kernel"></param>
        /// <param name="info"></param>
        /// <param name="onDestroying"></param>
        public virtual void Init(IActivator activator, IKernel kernel, IComponentInfo info, Action<IComponentInfo, object> onDestroying)
        {
            this.Activator = activator;
            this.Kernel = kernel;
            this.Info = info;
            OnDestroying = onDestroying;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public virtual object Get(IPreCreationContext ctx)
        {
            return Activator.Create(ctx);
        }

       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            OnDestroying = null;
        }
    }
}
